<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

// Basic parameter data
$fb_user_info		= request_var('fb_user_info', '');
$mode				= request_var('mode', '');

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');


$fb_user_info = htmlspecialchars_decode($fb_user_info);
$fb_user = json_decode($fb_user_info, true); // to make it an array  		
if ($fb_user == null)
{
	// unexpected error	
}	

if ($mode == 'submit')
{
	$name_option = request_var('name_option', '');
	$selected_name = utf8_clean_string(request_var('selected_name', '', true));
		
	// check if the selected name is OK
	if (username_exist($selected_name) == false)
	{
		// register the FB user	
		if (register_fb_user($fb_user['id'], $selected_name, $fb_user['first_name'], $fb_user['last_name'], '', $fb_user['link'], $fb_user['gender'], $fb_user['timezone'], $fb_user['locale'], $fb_user['verified']))
		{
			// refresh
			$url = append_sid("{$phpbb_root_path}/index.$phpEx", '');	
			redirect($url, false, true);
			echo "<a href='" . $url . "'>Redirecting...</a><br /><br />" . $url;
		}
	}
}


// list optional names
$name1 = $fb_user['name'] . '(FB)';
$name2 = $fb_user['username'];
$name3 = $fb_user['name'] . '(' . $fb_user['gender'] . ')';
$name4 = $fb_user['name'] . '(' . $fb_user['locale'] . ')';

if (username_exist($name1))
	$name1 = "";
if (username_exist($name2))
	$name2 = "";
if (username_exist($name3))
	$name3 = "";
if (username_exist($name4))
	$name4 = "";

$template->assign_vars(array(		
	'S_OPTIONAL_NAME_1'		=> $name1,		
	'S_OPTIONAL_NAME_2'		=> $name2,		
	'S_OPTIONAL_NAME_3'		=> $name3,		
	'S_OPTIONAL_NAME_4'		=> $name4,		
	'S_BASE_USERNAME'		=> $fb_user['name'],		
));


// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(	
	
	'S_NAME_CONFLICT_ACTION'		=> append_sid("{$phpbb_root_path}name_conflict.$phpEx", "mode=submit"),	
	'S_FB_USER_INFO'				=> htmlspecialchars($fb_user_info),
	
	'S_INCLUDE_JQUERY'				=> true,				// to instruct sv_overall_header.html to include jQuery. 
	'S_INCLUDE_JQUERY_UI'			=> true,				// to instruct sv_overall_header.html to include jQuery UI. 
	
));

// Output page
page_header($user->lang['SV_SELL']);

$template->set_filenames(array(
	'body' => 'name_conflict.html')
);

page_footer();

?>